#include "fps-zone.hh"

#include "../server.hh"

FpsZone::FpsZone (const Vector3f& pos,
		  const Vector3d& rotate,
		  const Vector3f& size,
		  Server* s):
  Bounding (pos, rotate, size),
  s_ (s)
{
  std::cout << "sending zones" << std::endl;
  //broadcasting the new zone to every players
  for (tbb::concurrent_hash_map<int, boost::shared_ptr<Client> >::const_iterator it = s->clientlist ()->begin ();
       it != s->clientlist ()->end (); ++it)
    it->second->nm ()->send (RType::GAME_ZONE, tonet ());
}

bool
FpsZone::intersect (Bounding* obj)
{
  //Alex's sphere collision code
  if ((*obj) () == SPHERE)
  {
    // First step => main bounding collision
    // To avoid dynamic cast => costly and we're sure that it's a sphere !
    BoundingSphere* sph = (BoundingSphere*) obj;
    double collision_distance = size_ (0) + sph->size () (0);
    Vector3f other_pos = sph->pos ();
    Vector3f buff = other_pos - pos_;
    
    double distance = buff.get_norme ();
    if (distance < collision_distance)
      return true;
  }
  return false;
}

bool
FpsZone::intersectWall (Plane& p)
{
  //FPS zones should not intersect map borders
  //We may get interested in this case

  (void) p;
  return false;
}

void
FpsZone::interact (Object* obj)
{
  s_->ask_fps (this, obj->playerid ());
  s_->move_obj (obj, this);
}

std::string
FpsZone::tonet () const
{
  std::string tosend;
  tosend += destroy_;
  tosend += pos_.tonet ();
  tosend += size_.tonet ();

  return tosend;
}
